package com.mz.service.sys;

import java.text.SimpleDateFormat;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.mz.base.util.DBUtil;
import com.mz.entity.SysMenu;

public class SysMenuService {
	
	/**
	 * 查询系统菜单列表
	 * @param page
	 * @param sysUsers
	 */
	public JSONArray findSysMenu(){
		DBUtil db = new DBUtil();
		String sql = "select * from sys_menu where status=1 and menuCode like '___' order by orderNum asc";
		JSONArray jsonMenu1 = db.selectToJsonArray(sql);
		
		JSONArray retJson = new JSONArray();
		for(int i=0,len=jsonMenu1.length(); i<len; i++){
			JSONObject json = jsonMenu1.getJSONObject(i);
			sql = "select * from sys_menu where status=1 and menuCode like '"+json.getString("menuCode")+"___' order by orderNum asc";
			JSONArray jsonMenu2 = db.selectToJsonArray(sql);
			
			json.put("list", jsonMenu2);
			retJson.put(i, json);
		}
		
		return retJson;
	}
	/**
	 * 保存系统菜单信息
	 * @param sysMenu
	 * @return
	 */
	public int saveSysMenu(SysMenu sysMenu){
		DBUtil db = new DBUtil();
		int rows = 0;
		if(sysMenu.getMenuId()!=null){
			String sql = "update sys_menu set menuName='" + sysMenu.getMenuName()+ "', " +
											 "url='" + sysMenu.getUrl()+ "', " +
											 "parameter='" + sysMenu.getParameter()+ "', " +
											 "target='" + sysMenu.getTarget()+ "', " +
											 "orderNum=" + sysMenu.getOrderNum()+ ", " +
											 "status=" + sysMenu.getStatus() + ", " +
											 "updateTime=now(), " +
											 "updateUser=" + sysMenu.getUpdateUser() + 
						 " where menuId="+sysMenu.getMenuId();
			rows = db.update(sql);
		}else{
			String menuCode = this.getMenuCode(sysMenu.getMenuCode());
			
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String[] fields = new String[]{"menuCode", "menuName", "url", "parameter", "target",
					"orderNum", "status", "createTime", "createUser", "updateTime", "updateUser"};
			Object[] values = new Object[]{menuCode, sysMenu.getMenuName(), sysMenu.getUrl(),
					sysMenu.getParameter(), sysMenu.getTarget(), sysMenu.getOrderNum(), sysMenu.getStatus(),
					format.format(sysMenu.getCreateTime()), sysMenu.getCreateUser(), 
					format.format(sysMenu.getUpdateTime()), sysMenu.getUpdateUser()};
			rows = db.insert("sys_menu", fields, values);
		}
		
		SysRoleService sysRoleService = new SysRoleService();
		sysRoleService.findSysMenuJoinRole();
		
		return rows;
	}
	/**
	 * 获取系统菜单信息
	 * @param menuId
	 * @return
	 */
	public JSONObject getSysMenuById(Long menuId){
		DBUtil db = new DBUtil();
		String sql = "select * from sys_menu where menuId="+menuId;
		return db.selectToJson(sql);
	}
	/**
	 * 删除系统菜单账号
	 * @param menuId
	 * @param sessionMenuId
	 * @return
	 */
	public int deleteSysMenu(Long menuId, Long sessionMenuId){
		DBUtil db = new DBUtil();
		String sql = "update sys_menu set status=-1, updateTime=now(), updateUser="+sessionMenuId+" where menuId="+menuId;
		int row = db.update(sql);
		
		SysRoleService sysRoleService = new SysRoleService();
		sysRoleService.findSysMenuJoinRole();
		
		return row;
	}
	/**
	 * 创建menuCode编码
	 * @param superMenuCode
	 * @return
	 */
	private String getMenuCode(String superMenuCode){
		String sql = "select max(menuCode) menuCode from sys_menu";
		if(!StringUtils.isEmpty(superMenuCode)){
			sql += " where menuCode like '___'";
		}else{
			superMenuCode = "";
		}
		
		DBUtil db = new DBUtil();
		JSONObject json = db.selectToJson(sql);
		String menuCode = json.getString("menuCode");
		if(StringUtils.isEmpty(menuCode)){
			superMenuCode += "0000";
		}else{
			int code = Integer.valueOf(menuCode) + 1;
			if(code>99){
				superMenuCode += code;
			}else if(code>9){
				superMenuCode += "0" + code;
			}else{
				superMenuCode += "00" + code;
			}
		}
		return superMenuCode;
	}
}